layui.define(['layer', 'form', 'jquery', 'laytpl'], function (exports) {
  var $ = layui.$,
      layer = layui.layer,
      laytpl = layui.laytpl,
      isVip = false;

  var pageConf = {
    page: 0,
    size: 10
  }

  var salesPageConf = {
    page: 1,
    limit: 10
  }

  var userConf = {
    key_id: '',
    user_id: '',
    secret: ''
  }
  var works = {
    bindEvent: function () {

      $('.prod').addClass('on');
      $('input[type="submit"]').click(function () {
        var activeIndex = $(".nav ul li.on").index();
        if(activeIndex==4){
          works.initSalesPagination();
        }else{
          works.initPagination();
        }

      });

      $(".nav ul li a").click(function () {
        $(this).parent().addClass('on').siblings().removeClass('on');
        $('input[name="worksNameOrDciCode"]').val('');
        var id = $(this).data('id');
        if (id == 'passed') {
          $("#sales_table").show();
          $("#copyright_table").hide();
          works.initSalesPagination();
        } else {
          $("#sales_table").hide();
          $("#copyright_table").show();
          $('input[name="worksProcess"]').val(id);
          works.initPagination();
        }

      });
     /*
      $(document).on('click','td.first .img a,td.first .title a',function () {
        var id = $(this).data('id');
        if(typeof(id) == "undefined"){
          return false;
        }
        $.get('/copyright/getGoodsSn',{worksId:id},function (result) {
          if(result==""){
            return false;
          }
          var url = 'http://www.meisupic.com/goods.php?media_id='+result;
          window.open(url,"_blank");
        })
      })
      */
      $(document).on('click', '.ck a', function () {
        var id = $(this).data('id');
        location.href = '/copyright/manage/detail?id=' + id;
      })

      $(document).on('click', '.sales_ck a', function () {
        var id = $(this).data('id');
        location.href = '/copyright/manage/detail?goodsId=' + id;
      })

      //重新提交
      $(document).on('click', ".tj a", function () {
        var id = $(this).data('id');
        location.href = '/copyright/register/edit?registerId=' + id;
      })

      $(document).on('click', ".xg a", function () {
        var id = $(this).data('id');
        location.href = '/copyright/register/edit?registerId=' + id;
      })

      $(document).on('click', ".monitor_li a,.sales_monitor_li a", function () {
        if(!isVip){
          layer.msg('您不是vip，没有权限操作');
          return false;
        }
        var id = $(this).data('id');
        $.post('/copyright/monitor/save', {worksId: id}, function (result) {
          if (result.success) {
            layer.msg("操作成功");
          }
        })
      })

      $(document).on("click", ".sc a", function () {
        var id = $(this).data('id');
        console.log(id);

        layer.open({
          type: 1,
          title: false,
          closeBtn: 0,
          btn: ['取消', '确定'],
          btnAlign: 'c',
          skin: 'pe',
          area: ['500px', '260px'],
          content: $('.pop-tips'),
          yes: function (index, layero) {
            layer.close(index);
          },
          btn2: function (index, layero) {
            $.get('/copyright/deleteWorksById', {id: id}, function (data) {
              works.initPagination();
            })
          },
          success: function (layero, index) {
            $('#pop-tips .head a').on('click', function () {
              layer.close(index);
              return false;
            })
          }
        });

      });

    },
    initPagination: function () {
      $(".pages").pagination({
        showData: pageConf.size,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount:works.loadData(),
        callback: function (api) {
          pageConf.page = api.getCurrent() - 1;
          works.loadData();
        }
      }, function (api) {
        pageConf.page = (api.getCurrent() - 1);
        var totalPages = works.loadData();
        api.setPageCount(totalPages);
        api.filling(api.getCurrent())
      })
    },
    loadData: function () {
      var data = $('.search_form').serializeJSON(),
          totalPages = 0;
      var index = layer.load(1, {shade: [0.1, '#fff']});
      $.ajax({
        url: '/copyright/getWorksPage',
        data: $.extend(pageConf, data),
        type: 'post',
        dataType: 'json',
        async: false,
        success: function (result) {
          layer.close(index);
          if (result.content.length == 0) {
            $('.lists').hide();
            $('.nothing').show();
          } else {
            $('.lists').show();
            $('.nothing').hide();
            var getTpl = $('#tpl_row').html(),
                tbody = $("#tbody");
            laytpl(getTpl).render(result, function (html) {
              tbody.html(html);
            });
            totalPages = result.totalPages;
          }

        }
      })
      return totalPages;
    },
    initSalesPagination: function () {
      $(".pages").pagination({
        showData: pageConf.size,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount:works.loadSalesData(),
        callback: function (api) {
          salesPageConf.page = api.getCurrent();
          works.loadSalesData();
        }
      })
    },
    loadSalesData: function () {
      var data = $('.search_form').serializeJSON(),
          totalPages = 0;
      data.search_key = data.worksNameOrDciCode;
      var index = layer.load(1, {shade: [0.1, '#fff']});
      $.ajax({
        url: '/getUserSaleList',
        data: $.extend(salesPageConf, data, userConf),
        type: 'post',
        dataType: 'json',
        async: false,
        success: function (result) {
          layer.close(index);
          var total = result.total;
          if (total == 0) {
            $('.lists').hide();
            $('.nothing').show();
          } else {
            $('.lists').show();
            $('.nothing').hide();
            var getTpl = $('#salesBody').html(),
                tbody = $("#sales_tbody");
            laytpl(getTpl).render(result, function (html) {
              tbody.html(html);
            });
            totalPages = Math.ceil(total / 10);
          }
        }
      })
      return totalPages;
    },
    loadSecret: function () {
      $.getJSON('/copyright/getUserIdAndSecret', function (result) {
        userConf.key_id = result.keyId;
        userConf.user_id = result.userId;
        userConf.secret = result.secret;
      })
    },
    checkIsVip: function () {
      $.get('/account/isVip',function (result) {
        if(result){
          isVip = true;
        }else{
          isVip = false;
        }
      })
    }
  }
  works.bindEvent();
  works.loadSecret();
  works.initPagination();
  works.checkIsVip();
  exports('works', {});
});